package com.hehui.partimejob;


import android.annotation.SuppressLint;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.barnettwong.dragfloatactionbuttonlibrary.view.DragFloatActionButton;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.google.android.material.tabs.TabLayout;
import com.hehui.partimejob.fragment.CategoryFragment;
import com.hehui.partimejob.fragment.MainFragment;
import com.hehui.partimejob.fragment.MyFragment;
import com.hehui.partimejob.fragment.VerifyFragment;
import com.hehui.partimejob.websocket.WebClient;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ViewPager2 mainVp;
    private TabLayout mainNav;
    private DragFloatActionButton uploadButton;



    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        WebClient.initWebSocket(this,getSharedPreferences().getInt("uid",1));

        getNavView().hide();


        uploadButton = (DragFloatActionButton) findViewById(R.id.upload_button);
        mainVp = (ViewPager2) findViewById(R.id.mainVp);
        mainNav = (TabLayout) findViewById(R.id.mainNav);


        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UploadActivity.class);
            }
        });
        List<Fragment> mainFragmentList = new ArrayList<>();
        mainFragmentList.add(new MainFragment());
        mainFragmentList.add(new CategoryFragment());
        mainFragmentList.add(new VerifyFragment());
        mainFragmentList.add(new MyFragment());
        mainVp.setOffscreenPageLimit(mainFragmentList.size());
        mainVp.setUserInputEnabled(false);

        mainVp.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mainFragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return mainFragmentList.size();
            }
        });
        mainVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mainNav.selectTab(mainNav.getTabAt(position));
            }
        });
        String[] navItems = getResources().getStringArray(R.array.main_nav);
        @SuppressLint("Recycle") TypedArray navIconItems = getResources().obtainTypedArray(R.array.main_nav_icon);
        @SuppressLint("Recycle") TypedArray navIconItemsSelect = getResources().obtainTypedArray(R.array.main_nav_icon_select);

        for (int i = 0; i < navItems.length; i++) {
            mainNav.addTab(mainNav.newTab().setText(navItems[i]).setIcon(navIconItems.getDrawable(i)));
        }
        mainNav.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
               mainVp.setCurrentItem(mainNav.getSelectedTabPosition());
               tab.setIcon(navIconItemsSelect.getDrawable(mainNav.getSelectedTabPosition()));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
//        setTitle(R.string.rec);
//        RelativeLayout navRightView = getNavRightView();
//        ImageView headPhoto = new ImageView(this);
//        headPhoto.setImageResource(R.drawable.ic_launcher_background);
//        navRightView.addView(headPhoto);
    }
}